<?php
require_once("./session.php");
require_once("../include/global.php");


if(empty($_GET['action']))
{
print <<<HTML
<div id="linklist" style="width:100%; margin:0 auto;padding-bottom:15px;">
	<div class="admin_title" >链接管理</div>
	<div id="content" style="width:100%; margin-top:10px;">
		<table width="740" border="1" cellpadding="0" cellspacing="0" bordercolor="#ffffff">
		  <tr style="background:#6784A4">
			<td width="40" height="25" align="center">ID</td>
			<td width="100" align="center">链接名称</td>
			<td width="150" align="center">链接地址</td>
			<td width="250" align="center">Logo地址</td>
			<td width="30" align="center">顺序</td>
			<td colspan="2" align="center">操作</td>
		  </tr>
HTML;

$sql = "SELECT * FROM blog_link order by link_Order ";
$result = mysql_query($sql) or die(mysql_error());
while($rs = mysql_fetch_object($result))
{
print <<<HTML
  <tr>
    <form name=$rs->link_ID id=$rs->link_ID method="post" action="./main.php?do=links&action=save">
    <td height="20" align="center">$rs->link_ID<input name="link_ID" type="hidden" value="$rs->link_ID" /></td>
    <td align="center"><input name="link_Name" type="text" value="$rs->link_Name" /></td>
    <td align="center"><input name="link_Url" type="text" value="$rs->link_Url" /></td>
	<td align="center"><input name="link_Image" type="text" value="$rs->link_Image" size=40/></td>
	<td align="center"><input name="link_Order" type="text" value="$rs->link_Order" size=4 maxlength=4 /></td>
    <td width="30" align="center">
	<button type=submit style="border:0px;"><img src="./images/icon_save.gif" alt="保存修改" width="16" height="16" /></button></td>
    <td width="30" align="center">
	<a href="$phpself?do=links&action=delete&linkID=$rs->link_ID" ><img src="./images/icon_del.gif" alt="删除" width="16" height="16" /></a></td>
	</form>
  </tr>
HTML;
}
  
print <<<HTML
  <tr style="background:#6784A4">
    <td height="25" align="left" colspan="7">&nbsp;&nbsp;添加友情链接</td>
  </tr>
  <tr>
    <form name="addlink" id="addlink" method="post" action="./main.php?do=links&action=save">
    <td height="20" align="center"><input name="link_ID" type="hidden" value="0" />0</td>
    <td align="center"><input name="link_Name" type="text" value="" /></td>
    <td align="center"><input name="link_Url" type="text" value="" /></td>
	<td align="center"><input name="link_Image" type="text" value="" size=40/></td>
	<td align="center"><input name="link_Order" type="text" value="" size=4 maxlength=4 /></td>
	<td colspan="2" align="center">
	<button type=submit style="border:0px;"><img src="./images/icon_save.gif" alt="保存链接" width="16" height="16" /></button></td>
	</form>
  </tr>
</table>
<p></p>
  </div>
</div>
HTML;
}
else
{
    switch($_GET['action'])
	{
	    case "delete" :
		   if(!empty($_GET['linkID']) && is_numeric($_GET['linkID']))
		   {  
		        DelLink($_GET['linkID']);
				echo "<script>location.href='main.php?do=links';</script>"; 
		   }
		   break;
		   
		case "save" :
		   $link_ID    = intval($_POST['link_ID']);
		   $link_Name  = htmlspecialchars(my_addslashes($_POST['link_Name']));
		   $link_Url   = htmlspecialchars(my_addslashes($_POST['link_Url']));
		   $link_Image = htmlspecialchars(my_addslashes($_POST['link_Image']));
		   $link_Order = intval($_POST['link_Order']);
		   
		   $sql = "SELECT * from blog_link where link_ID = $link_ID ";
		   $result  = mysql_query($sql);
		   if(!mysql_num_rows($result))
		   {  
		       $sql = "INSERT INTO blog_link(link_ID,link_Name,link_Url,link_Image,link_Order)VALUES(0,'$link_Name','$link_Url','$link_Image','$link_Order')";
		   }else{
		       $sql = "UPDATE blog_link SET link_Name = '$link_Name',link_Url = '$link_Url',link_Image = '$link_Image',
link_Order = $link_Order WHERE link_ID = $link_ID LIMIT 1 ";
		   }
		   mysql_query($sql) or die(mysql_error());
		   echo "<script>location.href='main.php?do=links';</script>"; 
		   
		default:break;
	}
}
?>
